package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Attachment;
import com.sl.au.entity.AuditNode;
import com.sl.au.entity.ExdeBeforeContract;
import com.sl.au.entity.ExdeBeforeContractCounterSign;
import com.sl.au.entity.ExdeBeforeContractDetailParts;
import com.sl.au.entity.ExdeBeforeContractDetails;
import com.sl.au.entity.ExdeBeforeContractNotices;
import com.sl.au.entity.ExdeBeforeContractSpecialClause;
import com.sl.au.entity.ExdeBeforeContractWGDetail;
import com.sl.au.entity.Module;
import com.sl.au.entity.Quote;
import com.sl.au.entity.QuoteDevice;
import com.sl.au.entity.QuoteForeign;
import com.sl.au.entity.User;
import com.sl.au.repository.ExdeBeforeContractWGDetailRepository;
import com.sl.au.service.ActivitiService;
import com.sl.au.service.AttachmentService;
import com.sl.au.service.AuditNodeService;
import com.sl.au.service.ExdeBeforeContractService;
import com.sl.au.service.OperateLogService;
import com.sl.au.service.QuoteForeignService;
import com.sl.au.service.QuoteService;

/***
 * 
 * @author chuck
 * 20170221 签订前要点审批控制器 
 */
@RestController
@RequestMapping(value="/ui/beforecontract")
public class ExdeBeforeContractController extends BaseController{
	@Autowired
	ExdeBeforeContractService exdeBeforeContractService;
	
	@Autowired
	OperateLogService operateLogService;
	
	@Autowired
	ActivitiService activitiService;
	
	@Autowired
	AuditNodeService auditNodeService;
	
	@Autowired
	AttachmentService attachmentService;
	
	@Autowired
	QuoteService quoteService;

	@Autowired
	QuoteForeignService quoteForeignService;
	
	/**
	 * 打印方法
	 */
	@RequestMapping(value = "getPrintPage", method = RequestMethod.GET)
	public Map<String, Object> getPrintPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		String title = "";
		String pInstId = null;
		List<AuditNode> nodes = new ArrayList<>();
		ExdeBeforeContract exdeBeforeContract = exdeBeforeContractService.getExdeBeforeContractRepository()
				.getOne(id);
		if (null != exdeBeforeContract) {
			title = exdeBeforeContract.getcContractCode();
			pInstId = exdeBeforeContract.getProcessInstId();
		}
		if (title.length() > 0) {
			nodes = auditNodeService.getAll(pInstId, id);
		}

		//map.put("title", title);
		map.put("nodes", nodes);
		//Map<String, Object> condition = new HashMap<String, Object>(); 
		ExdeBeforeContract contracts = this.exdeBeforeContractService.getBeforeContract(id);//返回主表的内容
		String beforeId = contracts.getId();
		List<ExdeBeforeContractNotices> QT = exdeBeforeContractService.
				getExdeBeforeContractNoticesByBeforeId(0, 1000, beforeId);//其他注意条款
		List<ExdeBeforeContractSpecialClause> TSK = exdeBeforeContractService.
				getExdeBeforeContractSpecialClauseByBeforeId(0, 1000, beforeId);//合同特殊条款
		List<ExdeBeforeContractDetails> htbdw = exdeBeforeContractService.
				getExdeBeforeContractDetailsByBeforeId(0, 1000, beforeId);//合同标的物
		//List<ExdeBeforeContractDetailParts> htbdwzb = null;
		for(int i=0;i<htbdw.size();i++){
			String a = htbdw.get(i).getId();//取出合同标的物子表的id在子表的子表中查询
			List<ExdeBeforeContractDetailParts> htbdwzbTemp = exdeBeforeContractService.
					getExdeBeforeContractDetailPartsByDetailId(0, 1000, a);//获取合同标的物子表的子表中的
			//condition.put("htbdwzb"+i, htbdwzb);
			htbdw.get(i).setPatr(htbdwzbTemp);
		}
		List<ExdeBeforeContractWGDetail> beng = exdeBeforeContractService.
				getExdeBeforeContractWGDetailByBeforeId(0, 1000, beforeId);//获取泵表中的数据
		//map.put("htbdwzb", condition);
		map.put("beforeContract", contracts);
		map.put("qtzytk", QT);
		map.put("httstk", TSK);
		map.put("htbdw", htbdw);
		map.put("beng", beng);
		map.put("draw", request.getParameter("draw"));
		return map;
	}
	/***
	 * 进入列表界面
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(){
		Module module = this.getPageModule("contractbefore");
        ModelAndView modelAndView=createLayoutView("beforecontract/beforecontractindex");
        modelAndView.addObject("module", module);
        return modelAndView;
    }
	
	/**
	 * 新建
	 */
	@RequestMapping(value = "add")
	public ModelAndView addBeforeContract() {
		ModelAndView modelAndView = this.createLayoutView("beforecontract/beforecontract-add");
		Account a = this.getMyAccount();
		modelAndView.addObject("userId", a.getUser().getId());
		modelAndView.addObject("allSuperiorMen",exdeBeforeContractService.getAllSuperiorMenBySuperior(this.getMyAccount().getUser().getSaleArea().getSuperior()));
		modelAndView.addObject("beforeId", " ");
		return modelAndView;
	}
	/**
	 * 编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "edit/{id}")
	public ModelAndView editBeforeContract(@PathVariable String id) {
		ModelAndView modelAndView = this.createLayoutView("beforecontract/beforecontract-edit");
		Account a = this.getMyAccount();
		modelAndView.addObject("userId", a.getUser().getId());
		modelAndView.addObject("beforeId", id);
		modelAndView.addObject("allSuperiorMen",exdeBeforeContractService.getAllSuperiorMenBySuperior(this.getMyAccount().getUser().getSaleArea().getSuperior()));
		modelAndView.addObject("ExdeBeforeContract", this.exdeBeforeContractService.getBeforeContract(id));
		return modelAndView;
	}
	/**
	 * 编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "sp/{id}")
	public ModelAndView spBeforeContract(@PathVariable String id) {
		ModelAndView modelAndView = this.createLayoutView("beforecontract/beforecontract-submit");
		Account a = this.getMyAccount();
		modelAndView.addObject("userId", a.getUser().getId());
		modelAndView.addObject("beforeId", id);
		modelAndView.addObject("allSuperiorMen",exdeBeforeContractService.getAllSuperiorMenBySuperior(this.getMyAccount().getUser().getSaleArea().getSuperior()));
		modelAndView.addObject("ExdeBeforeContract", this.exdeBeforeContractService.getBeforeContract(id));
		return modelAndView;
	}
	
	/**
	 * 跳转到审批界面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "audit/{id}")
	public ModelAndView auditBeforeContract(@PathVariable String id) {
		ModelAndView modelAndView = this.createLayoutView("beforecontract/beforecontract-audit");
		Account a = this.getMyAccount();
		modelAndView.addObject("userId", a.getUser().getId());
		modelAndView.addObject("beforeId", id);
		modelAndView.addObject("allSuperiorMen",exdeBeforeContractService.getAllSuperiorMenBySuperior(this.getMyAccount().getUser().getSaleArea().getSuperior()));
		modelAndView.addObject("ExdeBeforeContract", this.exdeBeforeContractService.getBeforeContract(id));
		return modelAndView;
	}
	
	/**
	 * 删除签订前审批及其关联的数据
	 */
	@RequestMapping(value="delete",method = RequestMethod.GET)
	public Map<String, Object> deleteBeforeContract(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.hasText(id)) {
			exdeBeforeContractService.deleteContractById(id);
		}
		map.put("code", 0);
		return map;
	}
	
	/***
	 * 获取所有人员数据List
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "user/getpage", method = RequestMethod.POST)
	public Map<String, Object> getAllUserPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<User> contracts = exdeBeforeContractService.getAllUserList(start,length,key);
		long count = exdeBeforeContractService.getAllUserCount(key);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/***
	 * 获取合同签订前审批产品子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractdetails/getpage", method = RequestMethod.POST)
	public Map<String, Object> getExdeBeforeContractDetailsPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractDetails> contracts = exdeBeforeContractService.getExdeBeforeContractDetailsByBeforeId(start, length, beforeId);
		long count = exdeBeforeContractService.getExdeBeforeContractDetailsCountByBeforeId(beforeId);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/***
	 * 移动端 GET方式 获取合同签订前审批产品子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractdetails/getpage", method = RequestMethod.GET)
	public Map<String, Object> getMobileExdeBeforeContractDetailsPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractDetails> contracts = exdeBeforeContractService.getExdeBeforeContractDetailsByBeforeId(start, length, beforeId);
		map.put("data", contracts);
		return map;
	}
	/**
	 * 获取合同签订前审批会签界面列表数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractdetailHQ/getpage", method = RequestMethod.POST)
	public Map<String, Object> getExdeBeforeContractHQDetail(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractCounterSign> contracts = exdeBeforeContractService.getBeforeContractsHQ(start, length, beforeId);
		long count = exdeBeforeContractService.getBeforeContractHQCount(beforeId);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/***
	 *  移动端 GET方式  获取合同签订前审批合同合同标的物子表的子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractdetailparts/getpage", method = RequestMethod.GET)
	public Map<String, Object> getMobileExdeBeforeContractDetailPartsPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeDetailId = request.getParameter("beforeDetailId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractDetailParts> contracts = exdeBeforeContractService.getExdeBeforeContractDetailPartsByDetailId(start, length, beforeDetailId);
		map.put("data", contracts);
		return map;
	}
	/***
	 * 获取合同签订前审批合同合同标的物子表的子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractdetailparts/getpage", method = RequestMethod.POST)
	public Map<String, Object> getExdeBeforeContractDetailPartsPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeDetailId = request.getParameter("beforeDetailId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractDetailParts> contracts = exdeBeforeContractService.getExdeBeforeContractDetailPartsByDetailId(start, length, beforeDetailId);
		long count = exdeBeforeContractService.getExdeBeforeContractDetailPartsCountByDetailId(beforeDetailId);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/***
	 * 获取合同签订前审批其它注意条款子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractnotices/getpage", method = RequestMethod.POST)
	public Map<String, Object> getExdeBeforeContractNoticesPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractNotices> contracts = exdeBeforeContractService.getExdeBeforeContractNoticesByBeforeId(start, length, beforeId);
		long count = exdeBeforeContractService.getExdeBeforeContractNoticesCountByBeforeId(beforeId);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/***
	 * 获取合同签订前审批合同特殊条款子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractspecialclause/getpage", method = RequestMethod.POST)
	public Map<String, Object> getExdeBeforeContractSpecialClausePage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractSpecialClause> contracts = exdeBeforeContractService.getExdeBeforeContractSpecialClauseByBeforeId(start, length, beforeId);
		long count = exdeBeforeContractService.getExdeBeforeContractSpecialClauseCountByBeforeId(beforeId);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/***
	 * 获取合同签订前审批外购件独家指定子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractwgdetail/getpage", method = RequestMethod.POST)
	public Map<String, Object> getExdeBeforeContractWGDetailPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractWGDetail> contracts = exdeBeforeContractService.getExdeBeforeContractWGDetailByBeforeId(start, length, beforeId);
		long count = exdeBeforeContractService.getExdeBeforeContractWGDetailCountByBeforeId(beforeId);
		map.put("data", contracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	/***
	 * 移动端 GET方式 获取合同签订前审批外购件独家指定子表List数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "exdebeforecontractwgdetail/getpage", method = RequestMethod.GET)
	public Map<String, Object> getMoblieExdeBeforeContractWGDetailPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String beforeId = request.getParameter("beforeId");
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<ExdeBeforeContractWGDetail> contracts = exdeBeforeContractService.getExdeBeforeContractWGDetailByBeforeId(start, length, beforeId);
		map.put("data", contracts);
		return map;
	}
	
	/***
	 * 获取报价单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "quotesearch/getpumgpage", method = RequestMethod.POST)
	public Map<String, Object> getQuotesearchGetpumgpage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		if(key==null) key = "";
		Module module = this.getPageModule("quoteMng");
		String dataFilter = module.getDataFilter();
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior();
		String areaId = a.getUser().getSaleArea().getId();
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("key", key);
		condition.put("dataFilter", dataFilter);
		condition.put("superior", superior);
		condition.put("areaId", areaId);
		condition.put("userId", userId);
		condition.put("state", "通过");
		List<Quote> quotes = quoteService.getQuote(start, length, condition, null, null);
		long count = quoteService.getQuoteCount(condition);
		map.put("data", quotes);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "/findQuoteId", method = RequestMethod.GET)
	public Map<String, Object> getMainPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String quoteId = request.getParameter("quoteId");
		String beforeId = request.getParameter("beforeId");
		//删除要点审批的合同标的物
		/*List<ExdeBeforeContractDetails> contracts = exdeBeforeContractService.getExdeBeforeContractDetailsByBeforeId(0, 1000, beforeId);
		String ids = "";
		if(contracts.size()>0){
			for(ExdeBeforeContractDetails contract: contracts){
				String id = contract.getId();
				ids += id + ",";
			}
			System.out.println(ids);
			ids = ids.substring(0, ids.length()-1);
			System.out.println(ids);
			String mainId= exdeBeforeContractService.deleteExdeBeforeContractDetails(ids);
			List<Float> list = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(mainId);
		}*/
		//获取报价单的泵
		List<QuoteDevice> quoteDevices = quoteService.getQuoteDevice(0, 1000, quoteId, null, null);
		for(QuoteDevice quoteDevice : quoteDevices){
			boolean isInit = true;
			ExdeBeforeContractDetails exdeBeforeContractDetails = new ExdeBeforeContractDetails();
			Account a = this.getMyAccount();
			String uuid = java.util.UUID.randomUUID().toString();
			exdeBeforeContractDetails.setId(uuid);
			exdeBeforeContractDetails.setbContractId(beforeId);
			exdeBeforeContractDetails.setcProductName(quoteDevice.getProductName());
			exdeBeforeContractDetails.setcProductType(quoteDevice.getProductModel());
			exdeBeforeContractDetails.setnCount(quoteDevice.getNumber());
			exdeBeforeContractDetails.setnAddOrReducePrice(0);
			exdeBeforeContractDetails.setnContractPrice(0);
			exdeBeforeContractDetails.setnOutPrice(0);
			exdeBeforeContractService.getExdeBeforeContractDetailsRepository().save(exdeBeforeContractDetails);
			if(isInit && a.getUser().getSaleArea().getSuperior().equals("成套处") == false){//初始化标的物子表的子表 非成套处
				exdeBeforeContractService.initDetailsDetailsInfo(exdeBeforeContractDetails);
			}
			List<Float> listFloat = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(exdeBeforeContractDetails.getbContractId());
			map.put("code", 0);
			map.put("isInit", isInit);
			map.put("list", listFloat);
		}
		map.put("id", beforeId);
		return map;
	}
	
	/***
	 * 获取签订前要点审批主表List
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> condition = new HashMap<String, String>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		String superior = a.getUser().getSaleArea().getSuperior();
		String areaName = a.getUser().getSaleArea().getAreaName();
		String name = "contractbefore";
		Module module = this.getPageModule(name);
		String dataFilter = module.getDataFilter();
		if (key == null) {
			key = "";
		}
		key = key.replaceAll("\\s+", "");
		if (userId == null) {
			userId = "";
		}
		if (superior == null) {
			superior = "";
		}
		if (areaName == null) {
			areaName = "";
		}
		if (dataFilter == null) {
			dataFilter = "";
		}
		condition.put("key", key);
		condition.put("userId", userId);
		condition.put("superior", superior);
		condition.put("areaName", areaName);
		condition.put("dataFilter", dataFilter);
		List<ExdeBeforeContract> contracts = exdeBeforeContractService.getBeforeContracts(start, length, condition);
		List<ExdeBeforeContract> newContracts =new ArrayList<ExdeBeforeContract>();
		for (ExdeBeforeContract r : contracts) {
			String parentId = r.getId();
			List<Attachment> c = attachmentService.getRepository().findByParentId(parentId);
			int num = c.size();
			r.setAttachNumber(num);
			newContracts.add(r);
		}
		
		long count = exdeBeforeContractService.getBeforeContractCount(condition);
		map.put("data", newContracts);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 新增、修改保存签订前审批主表
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContract(HttpServletRequest request, @RequestBody ExdeBeforeContract exdeBeforeContract) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		boolean isInit = false;
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContract.getId())) {
			isInit = true;
			exdeBeforeContract.setId(uuid);
		}
		exdeBeforeContractService.getExdeBeforeContractRepository().save(exdeBeforeContract);
		List<Float> list = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(exdeBeforeContract.getId());
		if(isInit){//初始化合同特殊条款、外购件独家制定
			exdeBeforeContractService.initBaseDetailsInfo(uuid,a.getUser().getSaleArea().getSuperior());
		}
		
		//保存日志
		String id=exdeBeforeContract.getId();
		User user=a.getUser();
		String cContractCode=exdeBeforeContract.getcContractCode();
		String content = "新增、编辑签订前要点审批信息【"+cContractCode+"】";
		operateLogService.log(user, "要点审批新增、编辑", content);
		map.put("code", 0);
		map.put("isInit", isInit);
		map.put("id", id);
		map.put("list", list);
		return map;
	}
	
	/**
	 * 新增、修改保存合同会签人信息
	 * @return
	 */
	@RequestMapping(value = "saveExdeBeforeContractCounterSign", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContractCounterSign(HttpServletRequest request, @RequestBody ExdeBeforeContractCounterSign exdeBeforeContractCounterSign) {
		Map<String, Object> map = new HashMap<String, Object>();
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContractCounterSign.getId())) {
			exdeBeforeContractCounterSign.setId(uuid);
		}
		exdeBeforeContractService.getExdeBeforeContractHQDetailRepository().save(exdeBeforeContractCounterSign);
		map.put("code", 0);
		map.put("id", exdeBeforeContractCounterSign.getId());
		return map;
	}
	
	/**
	 * 新增、修改保存合同标的物情况
	 * @return
	 */
	@RequestMapping(value = "saveExdeBeforeContractDetails", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContractDetails(HttpServletRequest request, @RequestBody ExdeBeforeContractDetails exdeBeforeContractDetails) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isInit = false;
		Account a = this.getMyAccount();
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContractDetails.getId())) {
			isInit = true;
			exdeBeforeContractDetails.setId(uuid);
		}
		exdeBeforeContractService.getExdeBeforeContractDetailsRepository().save(exdeBeforeContractDetails);
		if(isInit && a.getUser().getSaleArea().getSuperior().equals("成套处") == false){//初始化标的物子表的子表 非成套处
			exdeBeforeContractService.initDetailsDetailsInfo(exdeBeforeContractDetails);
		}
		List<Float> list = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(exdeBeforeContractDetails.getbContractId());
		map.put("code", 0);
		map.put("isInit", isInit);
		map.put("id", exdeBeforeContractDetails.getId());
		map.put("list", list);
		return map;
	}
	
	/**
	 * 新增、修改保存合同签订前审批合同合同标的物子表的子表
	 * @return
	 */
	@RequestMapping(value = "saveExdeBeforeContractDetailParts", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContractDetailParts(HttpServletRequest request, @RequestBody ExdeBeforeContractDetailParts exdeBeforeContractDetailParts) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isInit = false;
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContractDetailParts.getId())) {
			isInit = true;
			exdeBeforeContractDetailParts.setId(uuid);
		}
		String beforeDetailId = exdeBeforeContractDetailParts.getbContractDetailId();
		List<ExdeBeforeContractDetailParts> contracts = exdeBeforeContractService.getExdeBeforeContractDetailPartsByDetailId(0, Integer.MAX_VALUE, beforeDetailId);
		exdeBeforeContractService.saveExdeBeforeContractDetailParts(exdeBeforeContractDetailParts, contracts, isInit);
		//exdeBeforeContractService.getExdeBeforeContractDetailPartsRespository().save(exdeBeforeContractDetailParts);
		String ContractId=exdeBeforeContractService.sumExdeBeforeContractDetailsPrices(exdeBeforeContractDetailParts.getbContractDetailId());
		List<Float> list = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(ContractId);
		map.put("code", 0);
		map.put("id", exdeBeforeContractDetailParts.getId());
		map.put("list", list);
		System.out.println(list);
		return map;
	}
	
	/**
	 * 新增、修改保存合同签订前审批其它注意条款子表
	 * @return
	 */
	@RequestMapping(value = "saveExdeBeforeContractNotices", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContractNotices(HttpServletRequest request, @RequestBody ExdeBeforeContractNotices exdeBeforeContractNotices) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isInit = false;
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContractNotices.getId())) {
			isInit = true;
			exdeBeforeContractNotices.setId(uuid);
		}
		String beforeId = exdeBeforeContractNotices.getbContractId();
		List<ExdeBeforeContractNotices> contracts = exdeBeforeContractService.getExdeBeforeContractNoticesByBeforeId(0, Integer.MAX_VALUE, beforeId);
		exdeBeforeContractService.saveExdeBeforeContractNotices(exdeBeforeContractNotices, contracts, isInit);
		//exdeBeforeContractService.getExdeBeforeContractNoticesRepository().save(exdeBeforeContractNotices);
		map.put("code", 0);
		map.put("id", exdeBeforeContractNotices.getId());
		return map;
	}
	
	/**
	 * 新增、修改保存合同签订前审批合同特殊条款子表
	 * @return
	 */
	@RequestMapping(value = "saveExdeBeforeContractSpecialClause", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContractSpecialClause(HttpServletRequest request, @RequestBody ExdeBeforeContractSpecialClause exdeBeforeContractSpecialClause) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isInit = false;
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContractSpecialClause.getId())) {
			isInit = true;
			exdeBeforeContractSpecialClause.setId(uuid);
		}
		String beforeId = exdeBeforeContractSpecialClause.getbContractId();
		List<ExdeBeforeContractSpecialClause> list = exdeBeforeContractService.getExdeBeforeContractSpecialClauseByBeforeId(0, Integer.MAX_VALUE, beforeId);
		exdeBeforeContractService.saveExdeBeforeContractSpecialClause(exdeBeforeContractSpecialClause, list, isInit);
		//exdeBeforeContractService.getExdeBeforeContractSpecialClauseRepository().save(exdeBeforeContractSpecialClause);
		map.put("code", 0);
		map.put("id", exdeBeforeContractSpecialClause.getId());
		return map;
	}
	
	/**
	 * 新增、修改保存合同签订前审批外购件独家指定子表
	 * @return
	 */
	@RequestMapping(value = "saveExdeBeforeContractWGDetail", method = RequestMethod.POST)
	public Map<String, Object> saveExdeBeforeContractWGDetail(HttpServletRequest request, @RequestBody ExdeBeforeContractWGDetail exdeBeforeContractWGDetail) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isInit = false;
		String uuid = java.util.UUID.randomUUID().toString();
		if(!StringUtils.hasText(exdeBeforeContractWGDetail.getId())) {
			isInit = true;
			exdeBeforeContractWGDetail.setId(uuid);
		}
		String beforeId = exdeBeforeContractWGDetail.getbContractId();
		List<ExdeBeforeContractWGDetail> contracts = exdeBeforeContractService.getExdeBeforeContractWGDetailByBeforeId(0, Integer.MAX_VALUE, beforeId);
		exdeBeforeContractService.saveExdeBeforeContractWGDetail(exdeBeforeContractWGDetail, contracts, isInit);
		//exdeBeforeContractService.getExdeBeforeContractWGDetailRepository().save(exdeBeforeContractWGDetail);
		map.put("code", 0);
		map.put("id", exdeBeforeContractWGDetail.getId());
		return map;
	}
	
	/**
	 * 批量删除合同签订前审批会签人员子表
	 * @return
	 */
	@RequestMapping(value = "deleteExdeBeforeContractCounterSigns", method = RequestMethod.GET)
	public Map<String, Object> deleteExdeBeforeContractCounterSigns(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		exdeBeforeContractService.deleteExdeBeforeContractCounterSigns(ids);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 批量删除合同签订前审批产品子表
	 * @return
	 */
	@RequestMapping(value = "deleteExdeBeforeContractDetails", method = RequestMethod.GET)
	public Map<String, Object> deleteExdeBeforeContractDetails(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String mainId= exdeBeforeContractService.deleteExdeBeforeContractDetails(ids);
		List<Float> list = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(mainId);
		map.put("code", 0);
		map.put("list", list);
		return map;
	}
	
	/**
	 * 批量删除合同签订前审批合同合同标的物子表的子表
	 * @return
	 */
	@RequestMapping(value = "deleteExdeBeforeContractDetailParts", method = RequestMethod.GET)
	public Map<String, Object> deleteExdeBeforeContractDetailParts(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String detailId = exdeBeforeContractService.deleteExdeBeforeContractDetailParts(ids);
		exdeBeforeContractService.sumExdeBeforeContractDetailsPrices(detailId);
		String ContractId=exdeBeforeContractService.sumExdeBeforeContractDetailsPrices(detailId);
		List<Float> list = exdeBeforeContractService.sumExdeBeforeContractDetailsPricesZJ(ContractId);
		map.put("code", 0);
		map.put("list", list);
		return map;
	}
	
	/**
	 * 批量删除合同签订前审批其它注意条款子表
	 * @return
	 */
	@RequestMapping(value = "deleteExdeBeforeContractNotices", method = RequestMethod.GET)
	public Map<String, Object> deleteExdeBeforeContractNotices(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		exdeBeforeContractService.deleteExdeBeforeContractNotices(ids);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 批量删除合同签订前审批合同特殊条款子表
	 * @return
	 */
	@RequestMapping(value = "deleteExdeBeforeContractSpecialClause", method = RequestMethod.GET)
	public Map<String, Object> deleteExdeBeforeContractSpecialClause(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		exdeBeforeContractService.deleteExdeBeforeContractSpecialClause(ids);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 批量删除合同签订前审批外购件独家指定子表
	 * @return
	 */
	@RequestMapping(value = "deleteExdeBeforeContractWGDetail", method = RequestMethod.GET)
	public Map<String, Object> deleteExdeBeforeContractWGDetail(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		exdeBeforeContractService.deleteExdeBeforeContractWGDetail(ids);
		map.put("code", 0);
		return map;
	}
	
	/**
	 * 提交签订前要点审批
	 * 
	 * @param beforeId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/audit-submit/{beforeId}", method = RequestMethod.POST)
	public Map<String, Object> submitAudit(@PathVariable String beforeId, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		Map<String, String> variables = new HashMap<>();
		Account account = this.getMyAccount();
		int code = -1;

		String auditorNumber = request.getParameter("auditorNumber");
		String cProjectName = request.getParameter("cProjectName");
		String auditor1 = request.getParameter("auditor1");
		String auditor2 = request.getParameter("auditor2");
		String auditor3 = request.getParameter("auditor3");
		String auditor4 = request.getParameter("auditor4");
		String auditor5 = request.getParameter("auditor5");
		String auditor6 = request.getParameter("auditor6");
		String auditor7 = request.getParameter("auditor7");
		String auditor8 = request.getParameter("auditor8");

		if (null != beforeId && null != account && null != auditorNumber) {
			// 放置流程实例变量
			variables.put("starter", account.getUser().getId());
			variables.put("auditor1", auditor1);
			variables.put("auditor2", auditor2);
			variables.put("auditor3", auditor3);
			variables.put("auditor4", auditor4);
			variables.put("auditor5", auditor5);
			variables.put("auditor6", auditor6);
			variables.put("auditor7", auditor7);
			variables.put("auditor8", auditor8);
			variables.put("auditorNumber", auditorNumber);
			variables.put("auditState", "已提交");
			variables.put("entityName", "beforecontract");
			variables.put("processDefinitionName", "签订前要点审批");
			variables.put("entityId", beforeId);
			ProcessInstance pInst = activitiService.startProcessInstance("bid", account.getUser().getId(), variables);
			// 流程启动后，将其实例相关信息保存到业务实体中，以便查询
			exdeBeforeContractService.saveProcessStart(beforeId, pInst);
			// 判断会签节点是否存在改变签订前要点审批主表状态
			exdeBeforeContractService.updateExdeBeforeContractCounterSignState(beforeId);
			code = 0;
		}
		map.put("code", code);
		return map;
	}
	
	// 审批者页面
	@RequestMapping(value = "audit-view/{taskId}/{id}", method = RequestMethod.GET)
	public ModelAndView auditView(@PathVariable String taskId, @PathVariable String id) {
		ModelAndView modelAndView = createLayoutView("beforecontract/beforecontract-audit-view");
		if (id != null && taskId != null) {
			boolean isAudit = true;
			if (taskId.equals("0")){
				isAudit = false;
			}
			modelAndView.addObject("isAudit", isAudit);
			ExdeBeforeContract exdeBeforeContract = exdeBeforeContractService.getExdeBeforeContractRepository().findOne(id);
			modelAndView.addObject("ExdeBeforeContract", exdeBeforeContract);
			modelAndView.addObject("cContractCode", exdeBeforeContract.getcContractCode());
			modelAndView.addObject("beforeId", exdeBeforeContract.getId());
			modelAndView.addObject("taskId", taskId);
			modelAndView.addObject("auditnodes", auditNodeService.getRepository().getNodesById(exdeBeforeContract.getProcessInstId(),id));
		}
		return modelAndView;
	}
	
	// 移动端审批者页面
		@RequestMapping(value = "audit-view/{taskId}/{id}/json", method = RequestMethod.GET)
		public Map<String, Object> auditViewMoblie(@PathVariable String taskId, @PathVariable String id) {
			Map<String, Object> map = new HashMap<>();
			if (id != null && taskId != null) {
				boolean isAudit = true;
				if (taskId.equals("0")){
					isAudit = false;
				}
				map.put("isAudit", isAudit);
				ExdeBeforeContract exdeBeforeContract = exdeBeforeContractService.getExdeBeforeContractRepository().findOne(id);
				map.put("ExdeBeforeContract", exdeBeforeContract);
				map.put("cContractCode", exdeBeforeContract.getcContractCode());
				map.put("beforeId", exdeBeforeContract.getId());
				map.put("taskId", taskId);
				map.put("auditNodes", auditNodeService.getAll(exdeBeforeContract.getProcessInstId(),id));
			}
			return map;
		}
	
	/**
	 * 查看详情
	 */
	@RequestMapping(value = "check/{id}")
	public ModelAndView viewcheeck(@PathVariable String id) {
		ModelAndView modelAndView = this.createLayoutView("beforecontract/beforecontract-check");
		Account a = this.getMyAccount();
		modelAndView.addObject("userId", a.getUser().getId());
		modelAndView.addObject("beforeId", id);
		modelAndView.addObject("ExdeBeforeContract", this.exdeBeforeContractService.getBeforeContract(id));
		return modelAndView;
	}
	
	
	
}
